/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.flexengine.utils;

import com.flexengine.logger.Logger;
import com.flexengine.component.ProgressBar;
import com.flexengine.view.Texture;
import java.io.File;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;

/**
 * @author Luann R. Athayde
 * @version 1.0
 * @since 1.0
 */
public class TextureManager {

    private final HashMap<String, Texture> textureMap;

    private TextureManager() {
        this.textureMap = new HashMap<>();
    }

    private void recursiveLoad(File files[], ProgressBar pBar) {
        for (File tmp : files) {
            if (tmp.isDirectory()) {
                File fTmp[] = tmp.listFiles();
                recursiveLoad(fTmp, pBar);
            } else if (tmp.isFile()) {
                if (tmp.getName().endsWith(".png") || tmp.getName().endsWith(".jpg")
                        || tmp.getName().endsWith(".jpeg") || tmp.getName().endsWith(".bmp")) {
                    Logger.logMessage("TextureManager - loadTextures(): Texture founded...");
                    String name = tmp.getName();
                    String key = name.substring(0, name.indexOf('.'));
                    String file = tmp.getPath();
                    file = file.substring(11, file.length());
                    Logger.logMessage("TextureManager - loadTextures(): name = " + key);
                    Logger.logMessage("TextureManager - loadTextures(): file = " + file);
                    Texture tex = new Texture(name, file, 1);
                    Logger.logMessage("TextureManager - loadTextures(): texture = " + tex);
                    this.textureMap.put(key, tex);
                    if( pBar!=null ) {
                        pBar.incrementProgress();
                    }
                }
            }
        }
    }

    /**
     * Load all textures in recursive mode conatis in ./resources/folder and
     * subfolders...
     * @param folder
     * @param pBar
     */
    public void loadTextures(String folder, ProgressBar pBar) {
        Logger.logMessage("TextureManager - loadTextures(): Loading textures...");
        try {
            File fRes = new File(ResourceManager.getLocale(folder));
            Logger.logMessage("TextureManager - loadTextures(): path = " + fRes.getPath());
            File fTexs[] = fRes.listFiles();
            recursiveLoad(fTexs, pBar);
        } catch (Exception e) {
            Logger.logError("TextureManager - loadTextures("+folder+") = " + e);
            System.exit(0);
        }
    }

    /**
     * Load all textures in recursive mode conatis in ./resources/folder and
     * subfolders...
     *
     * @param path
     */
    public void loadTextures(String folder) {
        loadTextures("", null);
    }

    /**
     * Load all textures in recursive mode contains in ./resources/ and
     * subfolders...
     */
    public void loadTextures() {
        loadTextures("", null);
    }
    
    public long texturesToLoad() {
        return texturesToLoad("");
    }
    private long recursiveCount(File[] files) {
        long count = 0;
        for (File file : files) {
            if (file.isFile()) {
                if (isValidTexture(file)) {
                    count++;
                }
            } else {
                count += recursiveCount(file.listFiles());
            }
        }
        return count;
    }
    public long texturesToLoad(String folder) {
        try {
            File fRes = new File(ResourceManager.getLocale(folder));
            File fTexs[] = fRes.listFiles();
            return recursiveCount(fTexs);
        } catch (Exception e) {
            Logger.logError("TextureManager - texturesToLoad(): "+e);
        }
        return 0;
    }

    public int size() {
        return textureMap.size();
    }

    public boolean isEmpty() {
        return textureMap.isEmpty();
    }

    public boolean containsKey(String key) {
        return textureMap.containsKey(key);
    }

    public boolean containsValue(Texture value) {
        return textureMap.containsValue(value);
    }

    public Texture get(String key) {
        return textureMap.get(key);
    }

    public Texture get(String key, String path) {
        return get(key, path, Texture.DEFAULT_BUFFERS);
    }

    public Texture get(String keyName, String path, int buffer) {
        Texture getTex = textureMap.get(keyName);
        if (getTex == null) {
            getTex = new Texture(keyName, path, buffer);
            textureMap.put(keyName, getTex);
        }
        return getTex;
    }

    public Texture get(String keyName, InputStream in) {
        Texture getTex = textureMap.get(keyName);
        if (getTex == null) {
            getTex = new Texture(keyName, in);
            textureMap.put(keyName, getTex);
        }
        return getTex;
    }

    public Texture put(String key, Texture tex) {
        //System.out.println("Holding texture on map. Tex = "+tex);
        return textureMap.put(key, tex);
    }

    public Texture remove(String key) {
        return textureMap.remove(key);
    }

    public void clear() {
        textureMap.clear();
    }

    public Collection<Texture> values() {
        return textureMap.values();
    }

    public Set<String> getKeySet() {
        return textureMap.keySet();
    }

    public Texture load(String name, String path, int buff) {
        return new Texture(name, path, buff);
    }

    public static boolean isValidTexture(File file) {
        return file.getName().endsWith(".png") || file.getName().endsWith(".jpg")
                || file.getName().endsWith(".jpeg") || file.getName().endsWith(".bmp");
    }
    private static TextureManager instance;

    public static TextureManager getInstance() {
        if (instance == null) {
            instance = new TextureManager();
        }
        return instance;
    }
}
